/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;


import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.Question;
import com.vng.zing.survey.thrift.model.State;
import com.vng.zing.survey.thrift.model.SurveyItem;
import com.vng.zing.survey.thrift.model.SurveyItemResult;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vng.survey.model.CommonModel;
import vng.survey.model.RenderModel;


/**
 *
 * @author namhh
 */
public class ResponseController extends BaseController {

    @Override
    public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {
        if (req.getParameter("id") == null) {
            return "";
        }
        int id =  new Random().nextInt(10);
        int idSurvey = Integer.parseInt(req.getParameter("id"));
        String content = "";
        TemplateDataDictionary dic = TemplateDictionary.create();
        SurveyItemResult surveyresult = null;
//        List<String> list = new ArrayList<String>();
//        list.add("1");
//        list.add("2");
//        Short a = 3;
//        Short b = 5;
//        Question question1 = new Question(a, content, content, true, QuesType.SINGLE_SELECTION, list);
//        Question question2 = new Question(b, content, content, true, QuesType.SINGLE_SELECTION, list);
//        Question question3 = new Question(new Short("7"), content, content, true, QuesType.MULTI_CHOICE, list);
//        Question question4 = new Question(new Short("8"), content, content, true, QuesType.MULTI_LINE, list);
//        List<Question> listQues = new ArrayList<Question>();
//        listQues.add(question1);
//        listQues.add(question2);
//        listQues.add(question3);
//        listQues.add(question4);
        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Trả lời khảo sát"));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));
      

        SurveyClient surveyclient = SurveyClient.getInstance();
        surveyresult = surveyclient.getSurvey(idSurvey);
        if (surveyresult.getErro() != com.vng.zing.survey.thrift.model.Error.SUCCESS ) {
            content = CommonModel.getError(dic, "Khảo sát không tồn tại!");
            return content;
        }
        
        SurveyItem survey = surveyresult.getSurveyItem();
//        int date = (int) (new Date().getTime());
//        if (surveyresult.getSurveyItem().getBeginDay() > date || surveyresult.getSurveyItem().getEndDay() < date) {
//            content = CommonModel.getError(dic, "Khảo sát ở trạng thái bị đóng!");
//            return content;
//
//        }
        State state = surveyclient.checkUser(idSurvey, id);
//        survey = new SurveyItem(1, "nghien cuu", "des", listQues, date, date, date, a, true, true);
//        req.setAttribute("id", "2555");

        

        dic.setVariable("ID_SURVEY", String.format("%s", idSurvey));
        dic.setVariable("TITLE", survey.getTitle());
        dic.setVariable("DESCRIPTION", survey.getDescription());
        dic.setVariable("BEGINDAY", CommonModel.getDate(survey.getBeginDay()));
        dic.setVariable("ENDDAY", CommonModel.getDate(survey.getEndDay()));
        dic.setVariable("POINT", String.format("%s", survey.getPoint()));
//            if(state== State.RESPONSE){
//                dic.setVariable("DISABLE_SUBMIT", "disabled");
//            }
        int index = 1;
        for (Question question : survey.getQuestions()) {
            TemplateDataDictionary sliceQuestions = dic.addSection("SLICE_QUESTION");
            sliceQuestions.setVariable("ID_QUESTION", String.format("%s", question.getIdQues()));
            sliceQuestions.setVariable("INDEX", String.format("%s", index));
            sliceQuestions.setVariable("TITLE", question.getTitleQues());
            sliceQuestions.setVariable("HELP", question.getHelpText());
            if (question.isRequire()) {
                sliceQuestions.setVariable("REQUIRE", "(*)");
            } else {
                sliceQuestions.setVariable("REQUIRE", "");
            }
            sliceQuestions.setVariable("REQUIRE_VALUE", String.format("%s", question.isRequire()));
            if (question.getType() == QuesType.SINGLE_LINE) {
                sliceQuestions.setVariable("ANSWER", CommonModel.getTextAnswer(TemplateDictionary.create()));
            }
            if (question.getType() == QuesType.MULTI_LINE) {
                String content1 = CommonModel.getParagraphAnswer(TemplateDictionary.create());
                sliceQuestions.setVariable("ANSWER", content1);
            }
            if (question.getType() == QuesType.SINGLE_SELECTION) {

                TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                for (String answer : question.getChoices()) {
                    TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                    sliceAnswers.setVariable("OPTION", answer);
                    sliceAnswers.setVariable("RADIO_NAME", String.format("%s", question.getIdQues()));
                }
                content = CommonModel.getSingleChoiceAnswer(dicAnswers);
                sliceQuestions.setVariable("ANSWER", content);

            }

            if (question.getType() == QuesType.MULTI_CHOICE) {

                TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                for (String answer : question.getChoices()) {
                    TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                    sliceAnswers.setVariable("OPTION", answer);
                }
                content = CommonModel.getMultipleChoiceAnswer(dicAnswers);
                sliceQuestions.setVariable("ANSWER", content);

            }

            index++;
        }

        Template template = RenderModel.getCTemplate("response");
        content = template.renderToString(dic);
        return content;
    }

}
